package com.smartstate.admin.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.smartstate.admin.biz.entity.dto.SyslogSearchDto;
import com.smartstate.admin.biz.service.ApiLoggerService;
import com.smartstate.common.base.entity.supperAdmin.Syslog;
import com.smartstate.common.base.httpResult.ResultVO;
import com.smartstate.common.base.httpResult.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author runner-up
 */
@RestController
@RequestMapping("/log")
@Api(tags = "日志管理")
public class ApiLoggerController extends BaseController {

    @Resource
    private ApiLoggerService apiLoggerService;

    @GetMapping("/web/list")
    @ApiOperation(value = "查询日志管理列表分页", notes = "查询日志管理列表分页")
    public ResultVO getApiLoggerPage(@ModelAttribute SyslogSearchDto params){
        return ResultVO.success(apiLoggerService.queryApiLoggerList(params));
    }

    //@SysLog("删除日志")
    @DeleteMapping(value = "/web/del/{id}")
    @ApiOperation(value = "删除日志管理", notes = "删除日志管理")
    public ResultVO deleteApiLogger(@ApiParam(value = "日志id", required = true) @PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return ResultVO.error("id不能为空");
        }
        if (apiLoggerService.deleteApiLogger(id)) {
            return ResultVO.success("删除成功");
        } else {
            return ResultVO.error("删除失败");
        }
    }


    @DeleteMapping(value = "/web/delBatch")
    @ApiOperation(value = "批量删除日志管理", notes = "批量删除日志管理")
    public ResultVO deleteBatchApiLogger(@RequestBody String ids) {
        if (StringUtils.isBlank(ids)) {
            return ResultVO.error("参数不能为空");
        }
        List<String> idList = JSON.parseArray(((JSONArray) JSON.parseObject(ids).get("ids")).toJSONString(), String.class);
        if (apiLoggerService.deleteBatchApiLogger(idList)) {
            return ResultVO.success("批量删除成功");
        } else {
            return ResultVO.error("批量删除失败");
        }
    }

    /**
     * 插入日志
     *
     * @param syslog 日志实体
     * @return ApiResponse
     */
    @PostMapping("/info/add")
    public ResultVO saveLog(@Validated @RequestBody Syslog syslog) {
        return ResultVO.success(apiLoggerService.saveLog(syslog));
    }
}
